<?php


class Quark
{
    private $cookie ="夸克网盘CK";
    public function getShareInfo($shareUrl){
        //https://pan.quark.cn/s/74f5cbfc9bc2#/list/share
        preg_match('|\/s\/(\w+)|',$shareUrl,$shareUrlArr);
        if (empty($shareUrlArr[1])){
            return false;
        }
        $code = $shareUrlArr[1];
        $md5 = md5($code.'Kuakewangpan&^*') ;
        $header[]="Content-Type: application/json";
        $header[]="Accept: application/json, text/plain, */*";
        $header[]="Cache-Control: no-cache";
        $header[]="Host: drive-pc.quark.cn";
        $header[]="User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36";
        $header[]="Referer: ".$shareUrl;
        $postUrl="https://drive-pc.quark.cn/1/clouddrive/share/sharepage/token?pr=ucpro&fr=pc&uc_param_str=&__dt=".$this::random_str(3,1)."&__t=".$this::getMillisecond();
        $postdata = '{"pwd_id":"'.$code.'","passcode":""}';
        $data =   $this::getCurl($postUrl,$postdata,$this->cookie,$header);
        $dataArr =json_decode($data,true);
        if ($dataArr['status']==200){
            $stoken = $dataArr['data']['stoken'];
            $title = $dataArr['data']['title'];
            $selectCount =  db('netdisk')->where(array(
                'title'=>$title,
                'type'=>1
            ))
                ->count();
            if ($selectCount>=1){
                return true;
            }
            $postUrl ="https://drive-pc.quark.cn/1/clouddrive/file/search?pr=ucpro&fr=pc&uc_param_str=&q=".urlencode($title)."&_page=1&_size=50&_fetch_total=1&_sort=file_type:desc,updated_at:desc&_is_hl=1";
            $data =   $this::getCurl($postUrl,0,$this->cookie,$header);
            $dataArr =json_decode($data,true);
            $sscount = sizeof($dataArr['data']['list']);
            for ($s=0;$s<$sscount;$s++){
                $pdir_fid = $dataArr['data']['list'][$s]['pdir_fid'];
                if("d00391b750c84d3fa5f137a826409c42"==$pdir_fid){
                    $newFileId = $dataArr['data']['list'][$s]['fid'];
                    break;
                }
            }
            if (!empty($newFileId)){
                goto a;
            }
            $postUrl =  "https://drive-pc.quark.cn/1/clouddrive/share/sharepage/detail?pr=ucpro&fr=pc&uc_param_str=&pwd_id={$code}&stoken=".urlencode($stoken)."&pdir_fid=0&force=0&_page=1&_size=50&_fetch_banner=1&_fetch_share=1&_fetch_total=1&_sort=file_type:asc,updated_at:desc&__dt=".$this::random_str(4,1)."&__t=".$this::getMillisecond();
            $data1 =   $this::getCurl($postUrl,0,$this->cookie,$header);
            $dataArr1 =json_decode($data1,true);
            if ($dataArr1['status']==200){
                if (empty($dataArr1['data']['list'][0]['fid'])){
                    return false;
                }

                $fid_list = $dataArr1['data']['list'][0]['fid'];
                $fid_token_list = $dataArr1['data']['list'][0]['share_fid_token'];
                $postdata ="{\"fid_list\":[\"$fid_list\"],\"fid_token_list\":[\"$fid_token_list\"],\"to_pdir_fid\":\"d00391b750c84d3fa5f137a826409c42\",\"pwd_id\":\"$code\",\"stoken\":\"$stoken\",\"pdir_fid\":\"0\",\"scene\":\"link\"}";
                $randomTime=$this::random_str(5,1);
                $postUrl="https://drive-pc.quark.cn/1/clouddrive/share/sharepage/save?pr=ucpro&fr=pc&uc_param_str=&__dt=".$randomTime."&__t=".$this::getMillisecond();
                $data =   $this::getCurl($postUrl,$postdata,$this->cookie,$header);
                $dataArr =json_decode($data,true);
                if ($dataArr['status']==200){
                    $ss=0;
                    do{
                        sleep(2);
                        $task_id = $dataArr['data']['task_id'];
                        $randomTime= $randomTime +$this::random_str(4,1);
                        $postUrl ="https://drive-pc.quark.cn/1/clouddrive/task?pr=ucpro&fr=pc&uc_param_str=&task_id=$task_id&retry_index=0&__dt=".$randomTime."&__t=".$this::getMillisecond();
                        $data =   $this::getCurl($postUrl,0,$this->cookie,$header);
                        $dataArr =json_decode($data,true);
                        if ($dataArr['data']['status']==3){
                            return false;
                        }
                        $ss++;
                    }while($ss<2 && empty($dataArr['data']['save_as']['save_as_top_fids'][0]));

                    if ($dataArr['status']==200 && !empty($dataArr['data']['save_as']['save_as_top_fids'][0])){
                        $newFileId = $dataArr['data']['save_as']['save_as_top_fids'][0];
                        a :
                        $postUrl = "https://drive-pc.quark.cn/1/clouddrive/share?pr=ucpro&fr=pc&uc_param_str=";
                        $postdata = "{\"fid_list\":[\"$newFileId\"],\"title\":\"$title\",\"url_type\":1,\"expired_type\":1}";
                        $data =   $this::getCurl($postUrl,$postdata,$this->cookie,$header);
                        $dataArr =json_decode($data,true);
                        if ($dataArr['status']==200 && !empty($dataArr['data']['task_id'])){
                            sleep(1);
                            $task_id = $dataArr['data']['task_id'];
                            $postUrl = "https://drive-pc.quark.cn/1/clouddrive/task?pr=ucpro&fr=pc&uc_param_str=&task_id=$task_id&retry_index=1";
                            $data =   $this::getCurl($postUrl,0,$this->cookie,$header);
                            $dataArr =json_decode($data,true);
                            if ($dataArr['status']==200 && !empty($dataArr['data']['share_id'])){
                                $share_id=$dataArr['data']['share_id'];
                                $postUrl = "https://drive-pc.quark.cn/1/clouddrive/share/password?pr=ucpro&fr=pc&uc_param_str=";
                                $postdata = "{\"share_id\":\"$share_id\"}";
                                $data =   $this::getCurl($postUrl,$postdata,$this->cookie,$header);
                                $dataArr =json_decode($data,true);
                                if ($dataArr['status']==200 && !empty($dataArr['data']['share_url'])){
                                    $share_url = $dataArr['data']['share_url'];
                                    $newTitle = $dataArr['data']['title'];
                                    $newCode = $dataArr['data']['pwd_id'];
                                    $dbArr = array(
                                        'title'=>$title,
                                        'code'=>$code,
                                        'url'=>$shareUrl,
                                        'md5'=>$md5,
                                        'new_code'=>$newCode,
                                        'new_title'=>$newTitle,
                                        'new_url'=>$share_url,
                                        'file_id'=>$newFileId,
                                        'type'=>1,
                                    );
                                    db('netdisk')->insert($dbArr,true);
                                    return $dbArr;
                                }else{
                                    return false;
                                }
                            }else{
                                return false;
                            }
                        }else{
                            return false;
                        }
                    }else{
                        return false;
                    }
                }else{
                    return false;
                }
            }else{
                return false;
            }
        }else{
            return false;
        }
    }
    public static   function getCurl($url, $post = 0, $cookie = 0, $isIp=0,$header = 0, $nobaody = 0, $method = 'GET'){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        if (!empty($isIp)){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }
        if ($post==1) {
            curl_setopt($ch, CURLOPT_POST, 1);
        }elseif($post){
            if ($method !='PUT'){
                $method = 'POST';
            }
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        }
        if ($header) {
            curl_setopt($ch, CURLOPT_HEADER, true);
        }
        if ($cookie) {
            curl_setopt($ch, CURLOPT_COOKIE, $cookie);
        }
        if ($nobaody) {
            curl_setopt($ch, CURLOPT_NOBODY, 1);
        }

        curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $ret = curl_exec($ch);
        curl_close($ch);
        return $ret;
    }
}